package com.whale.cloud.util.hbase.sql.client;

import com.whale.cloud.util.hbase.HBaseProperties;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

@Component
@EnableConfigurationProperties(HBaseProperties.class)
public class HBaseConfig {

  private static Configuration configuration = null;

  private static Map<String, String> config = new HashMap<String, String>();

  private static Properties prop = new Properties();

  private static HBaseProperties hBaseProperties;

  public static void set(String name, String value) {
    if (name != null && name.length() > 0) {
      config.put(name, value);
    }
  }

  public static void sethBaseProperties(HBaseProperties hBaseProperties) {
    HBaseConfig.hBaseProperties = hBaseProperties;
  }

  public static Configuration getConfiguration() {
    if (configuration == null) {
      configuration = HBaseConfiguration.create();
    }
    configuration.set("hbase.rpc.engine", "org.apache.hadoop.hbase.ipc.SecureRpcEngine");
    configuration.set("hbase.zookeeper.quorum", hBaseProperties.getQuorum());
    configuration.set("hbase.master", hBaseProperties.getMaster());
    configuration.set("hbase.rpc.protection", hBaseProperties.getProtection());
    configuration.set("hbase.superuser", hBaseProperties.getSuperuser());
    configuration.set("zookeeper.znode.parent", hBaseProperties.getParent());
    configuration.set("hbase.zookeeper.property.clientPort", hBaseProperties.getClientPort());
    configuration.set("zookeeper.recovery.retry", "3");
    configuration.set("zookeeper.recovery.retry.intervalmill", "1");

    if (config != null && !config.isEmpty()) {
      for (String name : config.keySet()) {
        String value = config.get(name);
        configuration.set(name, value);
      }
    }

    return configuration;
  }
}
